﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.FieldComparers
{
    /// <summary>
    /// 
    /// </summary>
    public class OrganizationNameComparer : IAttributeComparer
    {
        private Dictionary<string,string> _SynonymDictionary;
        /// <summary>full name -> synonyms</summary>
        public Dictionary<string,string> SynonymDictionary
        {
            get
            {
                return this._SynonymDictionary;
            }
            set
            {
                this._SynonymDictionary = value;
            }
        }

        private List<string> _StopWords;
        /// <summary></summary>
        public List<string> StopWords
        {
            get
            {
                return this._StopWords;
            }
            set
            {
                this._StopWords = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public OrganizationNameComparer()
        {
            this._SynonymDictionary = new Dictionary<string, string>();
            // university 
            {
                string fullName1 = "university";
                List<string> synonyms1 = new List<string>();
                synonyms1.Add("university");
                synonyms1.Add("univ");
                synonyms1.Add("univ.");
                synonyms1.Add("u.");
                foreach(string synonym in synonyms1)
                    this._SynonymDictionary.Add(synonym, fullName1);
            }
            // institute
            {
                string fullName2 = "institute";
                List<string> synonyms2 = new List<string>();
                synonyms2.Add("institute");
                synonyms2.Add("inst");
                synonyms2.Add("inst.");
                synonyms2.Add("institution");
                foreach (string synonym in synonyms2)
                    this._SynonymDictionary.Add(synonym, fullName2);
            }
            // company 
            {
                string fullName3 = "inc";
                List<string> synonyms3 = new List<string>();
                synonyms3.Add("inc");
                synonyms3.Add("inc.");
                synonyms3.Add("incorporated.");
                synonyms3.Add("llc");
                synonyms3.Add("llc.");
                synonyms3.Add("co.");
                synonyms3.Add("co");
                foreach (string synonym in synonyms3)
                    this._SynonymDictionary.Add(synonym, fullName3);
            }
            this._StopWords=new List<string>();
            this._StopWords.Add("the");
            this._StopWords.Add(",");
            this._StopWords.Add("of");
            this._StopWords.Add("-");
        }


        public AttributeComparerType ComparerType
        {
            get { return AttributeComparerType.ClassComparer; }
        }

        /// <summary>
        /// 
        /// </summary>
        public SqlDbType FieldDbType
        {
            get { return SqlDbType.NText; }
            set { throw new NotImplementedException(); }
        }

        public double Compare(object value1, object value2, double penalty, SqlConnection conn,
                              string unevenDistTableName)
        {
            if (value1 == null || value2 == null || value1.ToString().Length == 0 || value2.ToString().Length == 0)
                return 0.0;
            string name1 = value1.ToString().ToLower();
            foreach(string stopWord in this._StopWords)
            {
                name1 = name1.Replace(stopWord, " ");
            }
            string[] words1 = StringUtil.Split(name1, " ");
            List<string> wordList1=new List<string>();
            foreach(string word in words1)
            {
                if(!string.IsNullOrEmpty(word) && 
                    !this._StopWords.Contains(word))
                {
                    if(this._SynonymDictionary.ContainsKey(word))
                        wordList1.Add(this._SynonymDictionary[word]);
                    else 
                        wordList1.Add(word);
                }
            }
            words1 = wordList1.ToArray();
            string name2 = value2.ToString().ToLower();
            foreach (string stopWord in this._StopWords)
            {
                name2 = name2.Replace(stopWord, " ");
            }
            string[] words2 = StringUtil.Split(name2, " ");
            List<string> wordList2 = new List<string>();
            foreach (string word in words2)
            {
                if (!string.IsNullOrEmpty(word) &&
                    !this._StopWords.Contains(word))
                {
                    if (this._SynonymDictionary.ContainsKey(word))
                        wordList2.Add(this._SynonymDictionary[word]);
                    else
                        wordList2.Add(word);
                }
            }
            words2 = wordList2.ToArray();
            string[] commonWords = StringUtil.Intersection(words1, words2);
            double compareScore = 0.0;
            if (commonWords != null && commonWords.Length > 0)
            {
                string[] allWords = StringUtil.Unions(words1, words2);
                return ((double) commonWords.Length)/allWords.Length;
            }
            if (compareScore == 0)
                compareScore = penalty;
            return compareScore;
        }

        public object Merge(List<object> values, SqlConnection conn, string unevenDistTableName)
        {
            if (values == null || values.Count == 0)
                return null;

            Dictionary<object, int> countByValues = new Dictionary<object, int>();
            foreach (object obj in values)
            {
                if (obj == null)
                    continue;

                if (!countByValues.ContainsKey(obj))
                    countByValues.Add(obj, 1);
                else
                    countByValues[obj] = countByValues[obj] + 1;
            }

            // check ssn9 first
            if (countByValues.Count > 0)
            {
                object mode = null;
                int count = 0;
                foreach (object obj in countByValues.Keys)
                {
                    if (countByValues[obj] > count)
                    {
                        count = countByValues[obj];
                        mode = obj;
                    }
                }
                return mode;
            }
            else
                return null;
        }
    }
}
